home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Aminet 22
/
Aminet 22 (1997)(GTI - Schatztruhe)[!][Dec 1997].iso
/
Aminet
/
dev
/
e
/
bezier.lha
/
bezier.e
< prev
next >
Wrap
Text File
|
1997-09-30
|
3KB
|
86 lines
OPT PREPROCESS
CONST WIDTH=640, HEIGHT=512, MODE=$29004 -> hires lace
->CONST WIDTH=640, HEIGHT=256, MODE=$29000 -> hires
->CONST WIDTH=320, HEIGHT=256, MODE=$21000 -> lowres
RAISE "SCR" IF OpenS()=0,
"WND" IF OpenW()=0,
"^C" IF CtrlC()<>0
PROC main() HANDLE
DEF s,w, p1_x,p1_y, p2_x,p2_y, p3_x,p3_y, p4_x,p4_y
Rnd(-Shl({arg},5)) -> fairly random ;)
s:=OpenS(WIDTH,HEIGHT,2,MODE,'Bezier Curves demo')
w:=OpenW(0,0,WIDTH,HEIGHT,0,$20800,'',s,15,0)
REPEAT
p1_x:=Shl(Rnd(WIDTH),16); p1_y:=Shl(Rnd(HEIGHT),16)
p2_x:=Shl(Rnd(WIDTH),16); p2_y:=Shl(Rnd(HEIGHT),16)
p3_x:=Shl(Rnd(WIDTH),16); p3_y:=Shl(Rnd(HEIGHT),16)
p4_x:=Shl(Rnd(WIDTH),16); p4_y:=Shl(Rnd(HEIGHT),16) ->(16,16) fixed point
Move(stdrast,0,0); ClearScreen(stdrast)
Line(Shr(p1_x,16),Shr(p1_y,16),Shr(p4_x,16),Shr(p4_y,16),2) -> P1 to P4
Line(Shr(p1_x,16),Shr(p1_y,16),Shr(p2_x,16),Shr(p2_y,16),2) -> P1 to P2
Line(Shr(p4_x,16),Shr(p4_y,16),Shr(p3_x,16),Shr(p3_y,16),2) -> P4 to P3
SetAPen(stdrast,1)
bezier(p1_x,p1_y, p2_x,p2_y, p3_x,p3_y, p4_x,p4_y)
Delay(80)
UNTIL CtrlC()
Raise(0)
EXCEPT
IF w THEN CloseW(w)
IF s THEN CloseS(s)
ENDPROC
#define AVG(x,y) Shr(x+y,1)
PROC bezier(p1_x,p1_y, p2_x,p2_y, p3_x,p3_y, p4_x,p4_y, level=0)
-> with thanks to Storm/Cydonia
-> All we do is divide the curve into two seperate curves, and repeat
-> this division until we have many curves (here we have 2^11 at maximum)
-> in which case we have enough accuracy to get away with drawing these
-> tiny curves as points.
-> P1 is the start point of the curve (or curve segment). P4 is the end
-> point. P2 and P3 are the control points of it. We divide the 'P' curve
-> into two curves, the 'L' curve (L1/L2/L3/L4) and the 'R' curve
-> (R1/R2/R3/R4). The L curve goes from the start point to the midpoint
-> of the P curve. The R curve goes from the midpoint to the end point of
-> the P curve.
DEF l1_x,l1_y, l2_x,l2_y, l3_x,l3_y, l4_x,l4_y,
r1_x,r1_y, r2_x,r2_y, r3_x,r3_y, r4_x,r4_y,
h_x,h_y
IF level<11
l1_x:=p1_x -> L1 = P1
l1_y:=p1_y
r4_x:=p4_x -> R4 = P4
r4_y:=p4_y
l2_x:=AVG(p1_x, p2_x) -> L2 = average(P1, P2)
l2_y:=AVG(p1_y, p2_y)
r3_x:=AVG(p3_x, p4_x) -> R3 = average(P3, P4)
r3_y:=AVG(p3_y, p4_y)
h_x:=AVG(p2_x, p3_x) -> H = average(P2, P3)
h_y:=AVG(p2_y, p3_y)
l3_x:=AVG(l2_x, h_x) -> L3 = average(L2, H)
l3_y:=AVG(l2_y, h_y)
r2_x:=AVG(r3_x, h_x) -> R2 = average(R3, H)
r2_y:=AVG(r3_y, h_y)
l4_x:=AVG(l3_x, r2_x) -> L4 = average(L3, R2)
l4_y:=AVG(l3_y, r2_y)
r1_x:=l4_x -> R1 = L4
r1_y:=l4_y
bezier(l1_x,l1_y, l2_x,l2_y, l3_x,l3_y, l4_x,l4_y, level+1) -> left
bezier(r1_x,r1_y, r2_x,r2_y, r3_x,r3_y, r4_x,r4_y, level+1) -> right
ELSE
WritePixel(stdrast,Shr(p1_x,16),Shr(p1_y,16))
ENDIF
ENDPROC